package com.im;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

// 注册窗口
@SuppressWarnings("serial")
public class RegWindow extends JDialog implements ActionListener {
    // 用户单击的按钮
    public enum ClickedButton {
        CANCEL, // "取消"按钮
        OK      // "确定"按钮
    }

    private JButton mButtonCancel;          // "取消"按钮
    private JButton mButtonOK;              // "确定"按钮
    private ClickedButton mClickedButton;   // 用户单击的按钮
    private JTextField mEditPassword;       // "密码"编辑框
    private JTextField mEditRepassword;     // "确认密码"编辑框
    private JTextField mEditUsername;       // "用户名"编辑框
    private int mIconIndex;                 // 头像索引
    private UserListView mListViewIcons;    // 头像列表控件
    private String mPassword;               // 密码
    private String mUsername;               // 用户名

    /**
     * 构造方法。
     */
    public RegWindow() {
        /***** 设置自身属性 *****/
        // 设置窗口标题
        super.setTitle(Const.getString("IDS_REG"));

        // 设置显示方式为模态
        super.setModal(true);

        // 设置窗口不能改变尺寸
        super.setResizable(false);

        // 设置
        super.setSize(Const.REG_WINDOW_WIDTH, Const.REG_WINDOW_HEIGHT);

        // 设置窗口居中
        super.setLocationRelativeTo(null);

        // 设置布局为网格袋布局
        final GridBagLayout gbl = new GridBagLayout();
        super.setLayout(gbl);

        /***** 创建控件 *****/
        // 创建网格袋布局约束对象
        final GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(Const.REG_WINDOW_CTRL_INSET_V, 
            Const.REG_WINDOW_CTRL_INSET_H, Const.REG_WINDOW_CTRL_INSET_V, Const.REG_WINDOW_CTRL_INSET_H);

        // "用户名"标签
        final JLabel labelUserName = new JLabel(Const.getString("IDS_USERNAME"));
        gbc.gridwidth = 1;
        gbl.setConstraints(labelUserName, gbc);
        super.add(labelUserName);

        // "用户名"编辑框
        mEditUsername = new JTextField(Const.REG_WINDOW_EDIT_WIDTH);
        mEditUsername.setDocument(new LimitDocument(Const.MAX_USERNAME_LEN));
        gbc.gridwidth = 0;
        gbl.setConstraints(mEditUsername, gbc);
        super.add(mEditUsername);

        // "密码"标签
        final JLabel labelPassword = new JLabel(Const.getString("IDS_PASSWORD"));
        gbc.gridwidth = 1;
        gbl.setConstraints(labelPassword, gbc);
        super.add(labelPassword);

        // "密码"编辑框
        mEditPassword = new JPasswordField(Const.REG_WINDOW_EDIT_WIDTH);
        mEditPassword.setDocument(new LimitDocument(Const.MAX_PASSWORD_LEN));
        gbc.gridwidth = 0;
        gbl.setConstraints(mEditPassword, gbc);
        super.add(mEditPassword);

        // "确认密码"标签
        final JLabel labelRepassword = new JLabel(Const.getString("IDS_REPASSWORD"));
        gbc.gridwidth = 1;
        gbl.setConstraints(labelRepassword, gbc);
        super.add(labelRepassword);

        // "确认密码"编辑框
        mEditRepassword = new JPasswordField(Const.REG_WINDOW_EDIT_WIDTH);
        gbc.gridwidth = 0;
        gbl.setConstraints(mEditRepassword, gbc);        
        super.add(mEditRepassword);
        
        // 头像列表控件
        final JLabel labelUserIcon = new JLabel(Const.getString("IDS_USER_ICON"));
        gbc.weightx = 2;
        gbl.setConstraints(labelUserIcon, gbc);
        super.add(labelUserIcon);

        mListViewIcons = new UserListView();
        gbc.weightx = 3;
        gbl.setConstraints(mListViewIcons, gbc);
        mListViewIcons.setVisibleRowCount(Const.REG_WINDOW_ICON_LIST_ROW_COUNT);
        mListViewIcons.setIconHorizontalSpacing(Const.REG_WINDOW_CTRL_INSET_V);
        mListViewIcons.setIconVerticalSpacing(Const.REG_WINDOW_CTRL_INSET_V);
        super.add(mListViewIcons);
        
        // 为列表控件添加图标
        for (int i = 0; i < Const.ICON_COUNT; i++) {
            mListViewIcons.addItem("", i, true);
        }

        // "确定"按钮
        mButtonOK = new JButton(Const.getString("IDS_OK"));
        mButtonOK.addActionListener(this);

        // "取消"按钮
        mButtonCancel = new JButton(Const.getString("IDS_CANCEL"));
        mButtonCancel.addActionListener(this);

        // 按钮面板
        final JPanel buttonPanle = new JPanel(new GridLayout(1, 2, Const.REG_WINDOW_CTRL_INSET_H, 0));
        buttonPanle.add(mButtonOK);
        buttonPanle.add(mButtonCancel);
        gbc.weightx = 4;
        gbc.gridwidth = 0;
        gbl.setConstraints(buttonPanle, gbc);
        super.add(buttonPanle);
        
        // 初始化成员 
        mClickedButton = ClickedButton.CANCEL;
        mIconIndex = -1;
    }

    /**
     * 按钮单击事件的响应方法。
     * @param e 事件相关信息
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == mButtonOK) {
            this.onOK();
        } else {
            this.onCancel();
        }
    }
    
    /**
     * 获取用户单击的按钮。
     * @return 用户单击的按钮
     */
    public ClickedButton getClickedButton() {
        return mClickedButton;
    }

    /**
     * 获取头像索引。
     * @return 头像索引
     */
    public int getIconIndex() {
        return mIconIndex;
    }

    /**
     * 获取密码。
     * @return 密码
     */
    public String getPassword() {
        return mPassword;
    }

    /**
     * 获取用户名。
     * @return 用户名
     */
    public String getUsername() {
        return mUsername;
    }

    /**
     * "取消"按钮的单击响应方法。
     */
    private void onCancel() {
        this.dispose();
    }
    
    /**
     * "确定"按钮的单击响应方法。
     */
    private void onOK() {
        // 检查用户名是否为空
        final String username = mEditUsername.getText();
        if (username.isEmpty()) {
            JOptionPane.showMessageDialog(this, Const.getString("IDS_USERNAME_EMPTY"), " ", JOptionPane.ERROR_MESSAGE);
            mEditUsername.requestFocus();
            return;
        }

        // 检查密码是否为空
        final String password = mEditPassword.getText();
        if (password.isEmpty()) {
            JOptionPane.showMessageDialog(this, Const.getString("IDS_PASSWORD_EMPTY"), " ", JOptionPane.ERROR_MESSAGE);
            mEditPassword.requestFocus();
            return;
        }

        // 检查两次密码是否一致
        final String repassword = mEditRepassword.getText();
        if (!password.equals(repassword)) {
            JOptionPane.showMessageDialog( this, Const.getString("IDS_PASSWORD_NOT_EQUAL"), " ", JOptionPane.ERROR_MESSAGE);
            mEditRepassword.requestFocus();
            return;
        }

        // 检查头像索引
        int itemIndex = mListViewIcons.getSelectedIndex();
        if (itemIndex == -1) {
            JOptionPane.showMessageDialog(this, Const.getString("IDS_ICON_NOT_SELECT"), " ", JOptionPane.ERROR_MESSAGE);
            mListViewIcons.requestFocus();
            return;
        }

        // 保存用户信息
        mUsername = username;
        mPassword = password;
        mIconIndex = itemIndex;

        // 关闭窗口
        mClickedButton = ClickedButton.OK;
        this.dispose();
    }
}
